0x03git 使用小记

1. 下载:

windows版
Mac版
Linux版
当然对于linux和Mac来说,通过apt-get install git 或者 mac下通过brew install git 来安装是最方便的。

2. 基本配置

1
2
git config --global user.name "Your Name Here"                   
git config --global user.email "your_email@youremail.com"

3. 生成SSH Keys

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cd ~/.ssh      
ssh-keygen -t rsa -C "your_email@youremail.com"
# Creates a new ssh key using the provided email

# Generating public/private rsa key pair.
# Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]

#Enter passphrase (empty for no passphrase): [Type a passphrase]
# Enter same passphrase again: [Type passphrase again]
#然后应该类似:
#Your identification has been saved in /Users/you/.ssh/id_rsa.
# Your public key has been saved in /Users/you/.ssh/id_rsa.pub.
# The key fingerprint is:
#01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@youremail.com

Windows Command Line:

1
type %userprofile%\.ssh\id_rsa.pub | clip

Windows PowerShell:

1
cat ~/.ssh/id_rsa.pub | clip

Mac:

1
pbcopy < ~/.ssh/id_rsa.pub

GNU/Linux (requires xclip):

1
xclip -sel clip < ~/.ssh/id_rsa.pub

4. 添加SSH KEYS到github或者bitbucket相关账户配置

1
pbcopy < ~/.ssh/id_rsa.pub

打开id_rsa.pub把其中内容复制过去。

5. git clone

1
git clone git@bitbucket.org:eagleon/acrm.git

6. git remote

1
git remote add remote_name git@bitbucket.org:eagleon/acrm.git

7. git push,pull,fetch

1
2
3
4
5
git pull remote_name branch_name               
git push remote_name branch_name
git pull origin master #从远程拉取到本地master分支
git fetch remote_name branch_name
git fetch origin master # 从远程拉取到本地的origin/master分支,但是不合并到master

8. 打tag,branch

1
2
3
4
git tag v0.1
git branch new_branch
git checkout -b dev origin/dev
git checkout -t origin/dev

9. add,commit相关

1
2
3
4
git status
git add .
git commit -a
git commit --amend #覆盖上次添加

10. merge

1
2
git merge <branch>  #把某个分支合并到现有分支
git merge --no-ff develop # 默认情况下,Git执行"快进式合并"(fast-farward merge),会直接将Master分支指向Develop分支。使用--no-ff参数后,会执行正常合并,在Master分支上生成一个新节点。为了保证版本演进的清晰,我们希望采用这种做法。

11. 缓存SSH密码

1
2
3
4
5
6
7
git config credential.helper store  #缓存到文件中,永久储存
git config credential.helper cache # linux 缓存到内存中
git config --global credential.helper wincred# windows 缓存到内存中,需要安装https://github.com/Microsoft/Git-Credential-Manager-for-Windows
git config --global credential.helper osxkeychain # mac 缓存到keychain中
git config --global credential.helper osxkeychain #linux mac windows都可以添加global参数
git config credential.helper 'cache --timeout=3600' #数字为保存的时间(秒,默认900) 在这个时间内输入的密码都是有效的,

12.丢弃本地修改

1
git checkout . && git clean -xdf

13. 把其他分支切换为master分支

1
git push origin +dev:master
1
2
3
4
5
6
7
8
9
10
Update the origin repository’s master branch with the dev branch, allowing non-fast-forward updates. This can leave unreferenced commits dangling in the origin repository. Consider the following situation, where a fast-forward is not possible:

o---o---o---A---B origin/master
\
X---Y---Z dev
The above command would change the origin repository to

A---B (unnamed branch)
/
o---o---o---X---Y---Z master

14. git alias

1
2
3
4
5
6
7
[alias]
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
last = log -1
co = checkout
ci = commit
br = branch
st = status

15. git push一次到多个仓库

方法一:

1
git remote set-url --add origin git@github.com:ehlxr/ehlxr-Hexo.git

方法二:

1
2
3
4
5
vim .git/config
[remote "origin"]
url = https://github.com/zxbetter/test.git
fetch = +refs/heads/*:refs/remotes/github/*
url = https://git.oschina.net/zxbetter/test.git

参考:
https://segmentfault.com/a/1190000011294144
https://ehlxr.me/2016/07/24/Git-%E5%90%8C%E6%97%B6-push-%E5%88%B0%E5%A4%9A%E4%B8%AA%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93/

Ref:

Set Up Git
Caching your GitHub password in Git
git-credential-winstore
Generating SSH keys
git - 简明指南
Git分支管理策略
Git远程操作详解